#include "CLocalTP_SQlite.h"

CLocalTP_SQlite::CLocalTP_SQlite(QString dbfilePath)
{
	m_db = QSqlDatabase::addDatabase("QSQLITE");
	m_db.setDatabaseName(dbfilePath);
}

CLocalTP_SQlite::~CLocalTP_SQlite()
{
	m_db.close();
}

QString CLocalTP_SQlite::providerName()
{
	return "LocalSqlite";
}

bool CLocalTP_SQlite::open()
{
	QDir appDir;
	
	// Database is already open
	if(m_db.isOpen())
		return true;

	// Try to open an existing database
	if(appDir.exists(m_db.databaseName()))
		return m_db.open();
	else
	{
		// Try to create a new database
		if(m_db.open())
		{
			QString querySt = "CREATE TABLE tTasks (";
			querySt += "id INTEGER PRIMARY KEY, name TEXT";
			querySt += ", addDate TEXT, desc TEXT, dueDate TEXT";
			querySt += ", priority NUMERIC, state NUMERIC";
			querySt += ");";
			
			QSqlQuery queryDb(querySt, m_db);
			return queryDb.exec();
		}
		else
			return false;
	}
}

bool CLocalTP_SQlite::close()
{
	m_db.close();
	return !m_db.isOpen();
}

QVector<STask*> CLocalTP_SQlite::getTasks()
{
	QVector<STask*> tasks;

	// Try to open base
	if(!m_db.isOpen())
		open();

	if(m_db.isOpen())
	{
		QSqlQuery query(m_db);
		if(query.exec("SELECT * FROM tTasks"))
		{
			int idNo = query.record().indexOf("id");
			int nameNo = query.record().indexOf("name");
			int addDateNo = query.record().indexOf("addDate");
			int descNo = query.record().indexOf("desc");
			int dueDateNo = query.record().indexOf("dueDate");
			int priorityNo = query.record().indexOf("priority");
			int stateNo = query.record().indexOf("state");
			
			while(query.next())
			{
				STask *tk = new STask;
				
				tk->distantId = query.value(idNo).toInt();
				tk->name = query.value(nameNo).toString();
				tk->description = query.value(descNo).toString();
				tk->addDate = QDateTime::fromString(query.value(addDateNo).toString());
				tk->dueDate = QDateTime::fromString(query.value(dueDateNo).toString());
				tk->priority = query.value(priorityNo).toInt();
								
				tasks.push_back(tk);
			}
		}
	}

	// Close db
	close();

	return tasks;
}

bool CLocalTP_SQlite::addTask(STask *task)
{
	bool result = false;

	// Try to open base
	if(!m_db.isOpen())
		open();

	if(m_db.isOpen())
	{
		QSqlQuery qrAdd(m_db);
		qrAdd.prepare("INSERT INTO tTasks (name, addDate, desc, dueDate, priority) "
			"VALUES (:name, :addDate, :desc, :dueDate, :priority)");
		qrAdd.bindValue(":name", task->name);
		qrAdd.bindValue(":addDate", task->addDate.toString());
		qrAdd.bindValue(":desc", task->description);
		qrAdd.bindValue(":dueDate", task->dueDate.toString());
		qrAdd.bindValue(":priority", task->priority);

		result = qrAdd.exec();
	}

	// Close db
	close();

	return result;
}

bool CLocalTP_SQlite::setChange(STask *task)
{
	bool result = false;

	// Try to open base
	if(!m_db.isOpen())
		open();

	if(m_db.isOpen())
	{
		QSqlQuery qrAdd(m_db);
		qrAdd.prepare("UPDATE tTasks "
			"SET name = :name, "
			"addDate = :addDate, "
			"desc = :desc, "
			"dueDate = :dueDate, "
			"priority = :priority "
			"WHERE id = :id");

		qrAdd.bindValue(":id", task->distantId);
		qrAdd.bindValue(":name", task->name);
		qrAdd.bindValue(":addDate", task->addDate.toString());
		qrAdd.bindValue(":desc", task->description);
		qrAdd.bindValue(":dueDate", task->dueDate.toString());
		qrAdd.bindValue(":priority", task->priority);

		result = qrAdd.exec();
	}

	// Close db
	close();

	return result;
}

bool CLocalTP_SQlite::delTask(STask *task)
{
	bool result = false;

	// Try to open base
	if(!m_db.isOpen())
		open();

	if(m_db.isOpen())
	{
		QSqlQuery qrDel(m_db);
		qrDel.prepare("DELETE FROM tTasks "
					  "WHERE id = :id");
		qrDel.bindValue(":id", task->distantId);

		result = qrDel.exec();
	}

	// Close db
	close();

	return result;
}

Q_EXPORT_PLUGIN2(localsqlite, CLocalTP_SQlite)
